package com.yang.doctorrecruitment.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yang.doctorrecruitment.dao.ApplyDAO;
import com.yang.doctorrecruitment.dao.MessageDAO;
import com.yang.doctorrecruitment.dto.Message;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class ReplyAction extends ActionSupport {
private JSON json;
    public String Reply(){
        HttpServletRequest request= (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        String reply=request.getParameter("Reply");
        String ToUserID=request.getParameter("UserID");//接收者UserID
        String JobID=request.getParameter("JobID");
        Map session=ActionContext.getContext().getSession();
        String UserID= (String) session.get("UserID");//发送者UserID
        Date date=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String Time=df.format(new Date());
        boolean res1= ApplyDAO.UpdataReply(reply,ToUserID,JobID);//更新apply表

        //注意这里有一个问题没有解决，就是没有做信息表的查重，可能重复插入
        Message message=new Message();
        message.setUserID(UserID);
        message.setToUserID(ToUserID);
        message.setMessage(reply);
        message.setTime(Time);
        boolean res2= MessageDAO.InsertMessage(message);
        JSONObject object=new JSONObject();
        if(res1&&res2){
            object.put("result","1");

        }else{
            object.put("result","0");
        }
        json=object;
        return SUCCESS;
    }


    public String ReplyReceive(){
        //回复收到的信息
        HttpServletRequest request= (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        String reply=request.getParameter("Reply");
        String ToUserID=request.getParameter("ToUserID");//发送者UserID
        String UserID= request.getParameter("UserID");//接收者UserID
        Date date=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String Time=df.format(new Date());
        //先判断有无
        JSONObject object=new JSONObject();
        boolean res=MessageDAO.hasReply(ToUserID,UserID);
        if(res){
            //表示之前已经有过回复，表中已经有记录，直接update即可
            Message message=new Message();
            message.setUserID(ToUserID);
            message.setToUserID(UserID);
            message.setMessage(reply);
            message.setTime(Time);
            boolean result=MessageDAO.updataReply(message);
            if(result){
                object.put("result","1");
            }else{
                object.put("result","0");
            }
        }else{
            //表示之前没有回复过，现在重新插入一条信息
            Message message=new Message();
            message.setUserID(ToUserID);
            message.setToUserID(UserID);
            message.setMessage(reply);
            message.setTime(Time);
            boolean result=MessageDAO.InsertMessage(message);
            if(result){
                object.put("result","1");
            }else{
                object.put("result","0");
            }
        }
        json=object;
        return SUCCESS;
    }
    public JSON getJson() {
        return json;
    }

    public void setJson(JSON json) {
        this.json = json;
    }
}
